﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    [MetadataType(typeof(EmpleadoMetaData))]
    public partial class Empleado {
        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public List<Empleado> Listar() {
            var result = from empleado in context.Empleadoes
                         select empleado;
            return result.ToList();
        }

        public Empleado Buscar(int iCodigo) {
            var result = from empleado in context.Empleadoes
                         where empleado.emCodigo == iCodigo
                         select empleado;
            return result.Count() > 0 ? result.First() : new Empleado();
        }

        public List<Empleado> Consultar(int? iCodigo,string sNombreCompleto, string sUsuario) {
            var query = from empleado in context.Empleadoes
                         select empleado;
            if (iCodigo == null && string.IsNullOrWhiteSpace(sNombreCompleto) && string.IsNullOrWhiteSpace(sUsuario))
            {
                return new List<Empleado>();
            }else{
                if (iCodigo != null) {
                    query = query.Where(w => w.emCodigo == iCodigo);
                }
                if (!string.IsNullOrWhiteSpace(sNombreCompleto))
                {
                    query = query.Where(w => w.emUsuario.Equals(sNombreCompleto));
                }
                if (!string.IsNullOrWhiteSpace(sUsuario))
                {
                    query = query.Where(w => w.emUsuario.Equals(sUsuario));
                }
                return query.ToList();
            }
        }

        public bool Guardar(Empleado empleado)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Empleado>(empleado);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(Empleado empleado)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<Empleado>(empleado);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(Empleado empleado)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<Empleado>(empleado);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class EmpleadoMetaData
    {
        [DisplayName("Codigo")]
        public int emCodigo { get; set; }
        [DisplayName("Nombre Completo")]
        public string emNombreCompleto { get; set; }
        [DisplayName("Usuario")]
        public string emUsuario { get; set; }
        [DisplayName("Password")]
        public string emPassword { get; set; }
    }
}
